<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>观察者模式</title>
</head>
<body>
  <script>
    class Dep {
      constructor () {
        this.subs = []
      }
      addSub (sub) {
        if (sub && sub.update) {
          this.subs.push(sub)
        }
      }
      notify () {
        this.subs.forEach(sub => {
          sub.update()
        })
      }
    }

    class Watcher {
      update () {
        console.log('update111')
      }
    }


    let dep = new Dep()
    let watcher = new Watcher()

    dep.addSub(watcher)
    dep.notify()

  </script>
</body>
</html>